package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import com.hanuor.onyx.Onyx;
import com.hanuor.onyx.hub.OnTaskCompletion;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {

    final HiLogLabel label = new HiLogLabel(HiLog.DEBUG, 0x00201, "Class");
    String url = "https://b.zol-img.com.cn/soft/6/968/ceMWVK5OOw9E6.jpg";
    byte[] bytes;
    Button BtnImgTags1;
    Button BtnImgTags2;
    Button BtnVideoTags1;
    Button BtnVideoTags2;
    Text tv;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        BtnImgTags1 = (Button) findComponentById(ResourceTable.Id_btn_img_tags1);
        BtnImgTags2 = (Button) findComponentById(ResourceTable.Id_btn_img_tags2);
        BtnVideoTags1 = (Button) findComponentById(ResourceTable.Id_btn_video_tags1);
        BtnVideoTags2 = (Button) findComponentById(ResourceTable.Id_btn_video_tags2);
        tv = (Text) findComponentById(ResourceTable.Id_textView);

    }

    @Override
    public void onActive() {
        super.onActive();
        //获取图片标签
        BtnImgTags1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                HiLog.debug(label, "点击了获取图片标签");
                Onyx.with(MainAbilitySlice.this).fromURL(url).getTagsfromApi(new OnTaskCompletion() {
                    @Override
                    public void onComplete(ArrayList<String> response) {
                        HiLog.debug(label, "" + response);
                        tv.setText(response.toString());
                    }
                });
            }
        });

        //获取图像标签及其发生的可能性
        BtnImgTags2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Onyx.with(MainAbilitySlice.this).fromURL(url).getTagsandProbability(new OnTaskCompletion() {
                    @Override
                    public void onComplete(ArrayList<String> response) {
                        HiLog.debug(label, "" + response);
                        tv.setText(response.toString());
                    }
                });
            }
        });

        //获取视频标签
        BtnVideoTags1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Onyx.with(MainAbilitySlice.this).fromVideoArray(bytes).getTagsfromApi(new OnTaskCompletion() {
                    @Override
                    public void onComplete(ArrayList<String> response) {
                        HiLog.debug(label, "" + response);
                        tv.setText(response.toString());
                    }
                });
            }
        });

        //获取视频标签及其发生的可能性
        BtnVideoTags2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Onyx.with(MainAbilitySlice.this).fromVideoArray(bytes).getTagsandProbability(new OnTaskCompletion() {
                    @Override
                    public void onComplete(ArrayList<String> response) {
                        HiLog.debug(label, "" + response);
                        tv.setText(response.toString());
                    }
                });
            }
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
